package com.tuyou.demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @描述:
 * @创建人: 涂有(tuyou_cd @ keruyun.com)
 * @创建时间: 2019-08-15
 */
@Slf4j
@Aspect
@Component
public class LogAop {

    public LogAop() {
        log.info("日志拦截器初始化...");
    }

    @Pointcut("execution(* com..*Controller.*(..))")
    public void controller(){}

    @Before("controller()")
    public void before(JoinPoint joinPoint) {
        log.info("请求方法：{}", joinPoint.toLongString());
        log.info("请求参数：{}", Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(value = "controller()", returning = "returnObj")
    public void after(Object returnObj) {
        log.info("返回结果：{}", returnObj == null ? null : returnObj.toString());
    }
}
